<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="ss" uri="/struts-tags"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="<%= path %>/public/extjs/resources/css/ext-all.css" />
<link rel="stylesheet" type="text/css" href="<%= path %>/public/css/icon.css" />
<link rel="stylesheet" type="text/css" href="<%= path %>/public/extux/LovCombo.css" />
</head>
<body topmargin="0">
<script type="text/javascript" src="<%= path %>/public/extjs/adapter/ext/ext-base.js"></script> 
<script type="text/javascript" src="<%= path %>/public/extjs/ext-all.js"></script> 
<script type="text/javascript" src="<%= path %>/public/extux/LovCombo.js"></script> 
<script type="text/javascript" src="<%= path %>/pages/script/progress.js"></script> 
<div id="tfield" style="margin:10 0 0 10;">
</div>
<div id="main" style="margin:10 0 0 10;"></div>
<script type="text/javascript" >
	Ext.BLANK_IMAGE_URL  =  '<%= path %>/public/extjs/resources/images/default/s.gif';
	Ext.QuickTips.init();
	var pathUrl = '<%= path %>'; //该变量必须存在
	Ext.onReady(function(){
		var form=new Ext.form.FormPanel({
			labelAlign:'right',
			title:'手工运行',
			border:false,
			items:[
				new Ext.form.DateField({
					fieldLabel:'数据日期',
					id:'dataDate',
					allowBlank:false,
					blankText:'数据日期不能为空',
					format:'Y-m-d',
					readOnly:true
				})
			]
		});
		form.render("tfield");
		function rendStatus(value){
			if(value=='1'){
				return '正在运行';
			}else if(value=='0'){
				return '空闲';
			}else if(value=='2'){
				return '错误';
			}
		}
		
		//列模型
		var sm = new Ext.grid.CheckboxSelectionModel({
				singleSelect:true
			});
		var columnModel=new Ext.grid.ColumnModel([
       		new Ext.grid.RowNumberer(),
       		sm,
       		{header:"作业流ID",dataIndex:"flowId",width:80,sortable: true},
       		{header:"作业流名称",dataIndex:"flowName",width:260,sortable: true},
       		{header:"依赖作业流",dataIndex:"dependId",width:100,sortable: true},
       		{header:"运行状态",dataIndex:"status",width:100,sortable: true,renderer:rendStatus},
       		{header:"进度",dataIndex:"avgRun",width:100,renderer :formatProgressBar,align:'center'}
       	]);
		//工具条
       	var ttbar=new Ext.Toolbar({
			width:600
         });
      	//数据来源
		var store=new Ext.data.Store({
			proxy:new Ext.data.HttpProxy({
			    url:pathUrl+"/listFlows.do"
			}),
			reader:new Ext.data.JsonReader(
				{
					totalProperty:"count",
					successProperty:"ready",
					root:"list"},
				[
				 {name:"flowId",mapping:"flowId"},
				 {name:"flowName",mapping:"flowName"},
				 {name:"status",mapping:"status"},
				 {name:"dependId",mapping:"dependId"},
				 {name:"avgRun",mapping:"avgRun"}
				])
		});

		
		var gridTable=new Ext.grid.GridPanel({
		  	title:'作业流列表',
		  	height : 390,
			width : 850,
			bodyStyle:'padding:5px',
			loadMask : true,
			pruneModifiedRecords:true,
			store:store,
			cm:columnModel,
			sm:sm,
			tbar:[{
   					text:"运 行",
   					handler:flowRun
	   			},'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
	   			{
   					text:"刷 新",
   					handler:reflush
	   			}
   			],
   			bbar:new Ext.PagingToolbar({    
   	            pageSize: 10,    
   	            store: store,    
   	            displayInfo: true,    
   	            displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',    
   	            emptyMsg: "没有记录"   
   	        })
			
		});
		function reflush(){
			store.reload();
		}
		store.load({params:{start:0,limit:10}}); 
		function flowRun(){
			var selection=gridTable.getSelectionModel().getSelections();
			if(selection.length==0){
				 Ext.MessageBox.alert('提示信息','请选择要运行的作业流.');
				 return;
			} 
			var totalTime=selection[0].get('avgRun');
			var da=Ext.getCmp('dataDate').value;
			if(!da){
				Ext.Msg.alert("提示信息","请输入数据日期！");
				return ;	
			}
			var sta=selection[0].get('status');
			if(sta=='1'){
				Ext.Msg.alert("提示信息","该作业流正在运行！");
				return ;	
			}
			var flowid=selection[0].get('flowId');
			Ext.Ajax.request({
				method:'post',
				params:{ flowId: flowid,dataDate: da},
				url:pathUrl+'/handRunFlow.do',
				callback : function(options, success, response) {
					var json = Ext.util.JSON.decode(response.responseText);
					if (success) {
						if(json.success){
							store.reload();
							if(totalTime!=0){
								var a=0;
								var task={
									run:function(){
										if(a>totalTime ){
											Ext.TaskMgr.stop(task);
											store.reload();
											return;
										}
										if(a%5==0&&a!=0){
											reflush();
										}
										if(a%5==1){
											var index=store.find('flowId',flowid);
											var stat=store.getAt(index).get('status');
											if(stat!=1){
												Ext.TaskMgr.stop(task);
												return;
											}
										}
										var percent = Math.ceil((a/totalTime)*100);
										formatProgress(percent,flowid);
										a++;
									},
									interval:1000
								};
								Ext.TaskMgr.start(task);
							}
						}else{
							Ext.MessageBox.alert('提示信息', json.message);
						}
					}
				},
	            waitMsg: '正在处理数据，请稍后……',
	            scope: this
			});
		}
		function flowStop(){
			var selection=gridTable.getSelectionModel().getSelections();
			if(selection.length==0){
				 Ext.MessageBox.alert('提示信息','请选择要停止的作业流.');
				 return;
			} 
			var sta=selection[0].get('status');
			if(sta!='1'){
				Ext.Msg.alert("提示信息","该作业流没在运行！");
				return ;	
			}
			Ext.Ajax.request({
				method:'post',
				params:{ flowId: selection[0].get('flowId')},
				url:pathUrl+'/stopThread.do',
				callback : function(options, success, response) {
					var json = Ext.util.JSON.decode(response.responseText);
					if (success) {
						if(json.success){
							Ext.MessageBox.alert('提示信息', '操作成功');
							store.reload();
						}else{
							Ext.MessageBox.alert('提示信息', json.message);
						}
					}
				},
	            waitMsg: '正在处理数据，请稍后……',
	            scope: this
			});
		}
		gridTable.render("main");
		function formatProgress(percent,flowid) {
			Ext.getDom('progressBar_' + flowid).style.width = percent + "%";
			Ext.getDom('progressText_' + flowid).innerHTML = percent + " %";
		};
	});
	
</script>
</body>

</html>

